package com.springboot_experiment_2.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * 自动配置类
 *
 * @author:HGF
 */
@EnableConfigurationProperties(CustomProperty.class)
public class Experiment2AutoConfig {

    /**
     * 能够控制某个configuration是否生效
     * prefix：前缀，name：属性名，其中name用来从application.properties中读取某个属性值
     * 如果该值为空，则返回false;
     * 如果值不为空，则将该值与havingValue指定的值进行比较，如果一样则返回true; 否则返回false。
     * 如果返回值为false，则该configuration不生效；为true则生效。
     */
    @Bean
    @ConditionalOnProperty(prefix = "experiment2.auto", name = "enable", havingValue = "true")
    CommandLineRunner createCustomCommandLineRunner(Environment environment) {
        return new CustomCommandLineRunner(environment);
    }
}
